package com.example.a2032.activity;

import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.a2032.R;
import com.example.a2032.Utils.BleSdk;
import com.example.a2032.Utils.Utils;
import com.example.a2032.Utils.WebSocketUtils;
import com.example.a2032.entity.PPGMeasureData;
import com.example.a2032.net.jstylenet.JstyleConst;
import com.example.a2032.net.jstylenet.NetSendUtils;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class BpMeasureActivity extends BaseActivity {
    @BindView(R.id.iv_meausre_ready)
    ImageView ivMeausreReady;
    @BindView(R.id.tv_measure_tips1)
    TextView tvMeasureTips1;
    @BindView(R.id.tvSbp)
    TextView tvSbp;
    @BindView(R.id.tvDbp)
    TextView tvDbp;
    @BindView(R.id.tvSpHeartRate)
    TextView tvSpHeartRate;
    @BindView(R.id.tvSbpValue)
    TextView tvSbpValue;
    @BindView(R.id.tvDbpValue)
    TextView tvDbpValue;
    @BindView(R.id.tv_bpValue)
    TextView tvBpValue;
    @BindView(R.id.tvHrValue)
    TextView tvHrValue;
    @BindView(R.id.tv_sbpUnit)
    TextView tvSbpUnit;
    @BindView(R.id.tv_dbpUnit)
    TextView tvDbpUnit;
    @BindView(R.id.tv_hrUnit)
    TextView tvHrUnit;
    @BindView(R.id.ProgressBar_bpMeasure)
    ProgressBar ProgressBarBpMeasure;
    @BindView(R.id.iv_meausre_tips)
    ImageView ivMeausreTips;
    @BindView(R.id.tv_measure_tips2)
    TextView tvMeasureTips2;
    @BindView(R.id.ll_measureTips)
    LinearLayout llMeasureTips;
    private Vibrator vibrator;
    private int time;
    private Disposable disposableFinish;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        KeepScreenOn();
        setContentView(R.layout.activity_bp_measure);
        ButterKnife.bind(this);
        init();
    }

    void showBpMeasureResult(int sbpValue, int dbpValue, int heartValue) {
        changeViewStatus(true);
        tvHrValue.setText(getShowMeasureValue(heartValue));
        tvSbpValue.setText(getShowMeasureValue(sbpValue));
        tvDbpValue.setText(getShowMeasureValue(dbpValue));
    }

    private String getShowMeasureValue(int measureValue) {

        return measureValue == 0 ? "--" : String.valueOf(measureValue);
    }

    void changeViewStatus(boolean isMeasureFinish) {
        tvMeasureTips1.setVisibility(isMeasureFinish ? View.GONE : View.VISIBLE);
        ProgressBarBpMeasure.setVisibility(isMeasureFinish ? View.GONE : View.VISIBLE);
        tvBpValue.setVisibility(isMeasureFinish ? View.GONE : View.VISIBLE);
        tvDbp.setVisibility(isMeasureFinish ? View.VISIBLE : View.GONE);
        tvSbp.setVisibility(isMeasureFinish ? View.VISIBLE : View.GONE);
        tvSpHeartRate.setVisibility(isMeasureFinish ? View.VISIBLE : View.GONE);
        tvDbpValue.setVisibility(isMeasureFinish ? View.VISIBLE : View.GONE);
        tvSbpValue.setVisibility(isMeasureFinish ? View.VISIBLE : View.GONE);
        tvHrValue.setVisibility(isMeasureFinish ? View.VISIBLE : View.GONE);
        tvDbpUnit.setVisibility(isMeasureFinish ? View.VISIBLE : View.GONE);
        tvSbpUnit.setVisibility(isMeasureFinish ? View.VISIBLE : View.GONE);
        tvHrUnit.setVisibility(isMeasureFinish ? View.VISIBLE : View.GONE);

    }

    String from = "";
    Disposable disposableMeasureTimer;

    private void init() {
        from = getIntent().getStringExtra(KEY_MeasureFrom);
        vibrator = (Vibrator) this.getSystemService(this.VIBRATOR_SERVICE);
        subscribe(this);
        Glide.with(this).asGif().load(R.mipmap.measure_bp).into(ivMeausreReady);
        changeViewStatus(false);
        time = getIntent().getIntExtra(KEY_MeasureTime, 2);
        if (time == 0) time = 2;
        Utils.startMeasureTimer(time, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {
                disposableMeasureTimer = d;
            }

            @Override
            public void onNext(Object o) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                finish();
            }
        });
        boolean isMeasure = WebSocketUtils.getInstance().isMeasuring();
        tvHrUnit.postDelayed(new Runnable() {
            @Override
            public void run() {
                sendData(BleSdk.enablePPGMeasure(1, true, time));
            }
        }, isMeasure ? 1000 : 0);


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unSubscribe(disposableMeasureTimer);
        unSubscribe(disposableFinish);
        sendData(BleSdk.enablePPGMeasure(1, false, 10));
        if(!measureFinish){
            upLoadData(0, 0, 0, JstyleConst.Measure_Finish);
        }
        KeepScreenOff();
    }

    @Override
    public void PPGMeasureData(PPGMeasureData ppgMeasureData) {
        super.PPGMeasureData(ppgMeasureData);
        int type = ppgMeasureData.getDataType();
        if (type == PPGMeasureData.MeasureData_PPGBP) {
            int wearStatus=ppgMeasureData.getWearStatus();
            if(wearStatus==PPGMeasureData.MeasureStatus_WearOff){
                llMeasureTips.setVisibility(View.VISIBLE);
                ivMeausreTips.setImageResource(R.drawable.bp_offtips);
                upLoadData(0, 0,0,JstyleConst.Measure_WearOff);
            }else {
                llMeasureTips.setVisibility(View.GONE);
                showBpData(ppgMeasureData);
            }


        }
    }
    boolean measureFinish;
    void showBpData(PPGMeasureData ppgMeasureData){
        int heartRate = ppgMeasureData.getHeartRate();
        int sbpValue = ppgMeasureData.getSbpValue();
        int dpbValue = ppgMeasureData.getDbpValue();
        int progress = ppgMeasureData.getMeasureProgress();
        ProgressBarBpMeasure.setProgress(progress * 2);
        if (progress == 50) {
            startFinishTimer();
            showBpMeasureResult(sbpValue, dpbValue, heartRate);
            upLoadData(sbpValue, dpbValue, heartRate, JstyleConst.Measure_Finish);
            Utils.saveBPData(this, ppgMeasureData, from);
            Utils.startVibrato(this);
            measureFinish=true;
        }
    }
    void upLoadData(int sbp, int dbp, int hrValue, int state) {
        String msg = NetSendUtils.realTimeBpData(sbp, dbp, hrValue, state, from);
        WebSocketUtils.getInstance().sendMsg(msg);
    }

    @Override
    protected void stopMeasure() {
        super.stopMeasure();
        finish();
    }

    void startFinishTimer() {
        Utils.startMeasureTimer(5, TimeUnit.SECONDS, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {
                disposableFinish = d;
            }

            @Override
            public void onNext(Object o) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                finish();
            }
        });
    }

}